'use strict';

/**
 * @ngdoc function
 * @name mymovies41FrontendApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the mymovies41FrontendApp
 */
angular.module('mymovies41FrontendApp')
  .controller('FilmCtrl', function ($scope, $cookies, $routeParams, $log, $http, $rootScope, FileUploader, Films, Studios) {

   var film = {}; 
   var coverImage;
   var screenshotImages = [];
   var coverImageUrl = $rootScope.baseUrl + '/films/' + $routeParams.id + '/cover';
   var screenshotImageBaseUrl = $rootScope.baseUrl + '/films/' + $routeParams.id + '/screenshots';

   $cookies.currentFilmId = $routeParams.id;
   $scope.showAddStudioFlag = false;
   $scope.coverImageUploader = new FileUploader({url: coverImageUrl, autoUpload: true, queueLimit: 1, alias: 'cover', removeAfterUpload: true });
   $scope.screenshotImagesUploader = new FileUploader({url: screenshotImageBaseUrl, autoUpload: true, alias: 'screenshots', removeAfterUpload: false });
   $scope.coverImageUrl = coverImageUrl;
   $scope.screenshotImageBaseUrl = screenshotImageBaseUrl;

   //reload cover image after uploading
   $scope.coverImageUploader.onCompleteItem = function() {
       var random = (new Date()).toString();
       coverImageUrl = coverImageUrl+ '?cb=' + random;
       $scope.coverImageUrl = coverImageUrl;
       $rootScope.imgTimestamp = random;
   };

   Films.getFilm($routeParams.id).then(function(response){
         film = response.data;
         $scope.film = film;
   }); 

   Films.getScreenshots($routeParams.id).then(function(response){
         var screenshots = response.data;
         $scope.screenshots = screenshots.filenames;
   }); 

   $scope.updateFilm = function() {
       var d = new Date();

       if ($scope.appendComment) {
           $scope.film.comment = $scope.film.comment + '\n\r[' + d.toDateString() + ']\n\r' + $scope.appendComment;
       }
       Films.updateFilm($scope.film).then(
           function(){
               $scope.showUpdateFilmResult = false;
               $scope.appendComment = '';
               $log.log('update film ' + $scope.film.id + ' successfully');
           },
           function(){
               $scope.showUpdateFilmResult = true;
               $log.log('update film ' + $scope.film.id + ' fails');
           }
       ); 
   };

   $scope.createFilm = function() {
       if ($scope.showAddStudioFlag) {
           var newStudio = { 'name': $scope.newStudio };
           $scope.film.studio = newStudio;
       }
       if ($scope.film.comment) {
           var d = new Date();
           $scope.film.comment =  '[' + d.toDateString() + ']\n\r' + $scope.film.comment;
       }
       Films.createFilm($scope.film, coverImage, screenshotImages).then(
           function(){
               $scope.createFilmSuccess = true;
               $log.log('create film ' + $scope.film.title + ' successfully');
           },
           function(){
               $scope.createFilmSuccess = false;
               $log.log('create film ' + $scope.film.title + ' fails');
           }
       ); 
   };

    $scope.listStudios = function() {
        Studios.getStudios().then(function(response){
            $scope.studios = response.data;
        });
    };

    $scope.setCoverImage = function(files) {
        coverImage = files[0];   
    };

    $scope.setScreenshotImages = function(files) {
        screenshotImages = files;
    };

    $scope.showAddStudio = function() {
        $scope.showAddStudioFlag = true;
    };

});

